<template>
  <div class="search-bar">
    <div class="search-input-wrapper">
      <input
        v-model="searchKeyword"
        type="text"
        class="search-input"
        placeholder="搜索音乐或歌手..."
        @input="handleInput"
        @keyup.enter="handleSearch"
      >
      <button 
        class="search-button"
        @click="handleSearch"
      >
        搜索
      </button>
    </div>
    
    <!-- 搜索建议列表 -->
    <div 
      v-if="showSuggestions && suggestions.length > 0" 
      class="suggestions-list"
    >
      <div
        v-for="suggestion in suggestions"
        :key="`${suggestion.title}-${suggestion.artist}`"
        class="suggestion-item"
        @click="selectSuggestion(suggestion)"
      >
        <span class="title">{{ suggestion.title }}</span>
        <span class="artist">{{ suggestion.artist }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { getMusicSuggestions } from '@/api/music'
import { useDebounceFn } from '@vueuse/core'

const emit = defineEmits<{
  (e: 'search', keyword: string): void
  (e: 'suggestion-selected', suggestion: { title: string, artist: string }): void
}>()

const searchKeyword = ref('')
const suggestions = ref<Array<{ title: string, artist: string }>>([])
const showSuggestions = ref(false)

// 防抖处理搜索建议请求
const fetchSuggestions = useDebounceFn(async (keyword: string) => {
  if (!keyword.trim()) {
    suggestions.value = []
    return
  }

  try {
    const response = await getMusicSuggestions(keyword)
    if (response.success && response.data) {
      suggestions.value = response.data
      showSuggestions.value = true
    }
  } catch (error) {
    console.error('获取搜索建议失败:', error)
  }
}, 300)

const handleInput = () => {
  fetchSuggestions(searchKeyword.value)
}

const handleSearch = () => {
  showSuggestions.value = false
  emit('search', searchKeyword.value)
}

const selectSuggestion = (suggestion: { title: string, artist: string }) => {
  searchKeyword.value = suggestion.title
  showSuggestions.value = false
  handleSearch()
}

// 点击外部时关闭建议列表
const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement
  if (!target.closest('.search-bar')) {
    showSuggestions.value = false
  }
}

// 监听点击事件
watch(() => showSuggestions.value, (newValue) => {
  if (newValue) {
    document.addEventListener('click', handleClickOutside)
  } else {
    document.removeEventListener('click', handleClickOutside)
  }
})
</script>

<style scoped lang="scss">
.search-bar {
  position: relative;
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
}

.search-input-wrapper {
  display: flex;
  gap: 10px;
}

.search-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid color-mix(in srgb, var(--theme-color) 30%, transparent);
  border-radius: 8px;
  font-size: 16px;
  outline: none;
  transition: all 0.3s ease;
  background: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);

  &::placeholder {
    color: color-mix(in srgb, white 60%, transparent);
  }
}

.search-input:focus {
  border-color: var(--theme-color);
  box-shadow: 0 4px 12px color-mix(in srgb, var(--theme-color) 20%, transparent);
}

.search-button {
  padding: 0 24px;
  background: var(--theme-gradient);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
  box-shadow: 0 4px 12px color-mix(in srgb, var(--theme-color) 20%, transparent);
}

.search-button:hover {
  background: linear-gradient(145deg, 
    color-mix(in srgb, var(--theme-color) 95%, white),
    var(--theme-color)
  );
  transform: translateY(-2px);
  box-shadow: 0 6px 16px color-mix(in srgb, var(--theme-color) 30%, transparent);
}

.suggestions-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: color-mix(in srgb, var(--bgc-color-2) 40%, transparent);
  border: 1px solid color-mix(in srgb, var(--theme-color) 20%, transparent);
  border-radius: 8px;
  margin-top: 4px;
  max-height: 300px;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.suggestion-item {
  padding: 12px 16px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.2s ease;
  border-bottom: 1px solid color-mix(in srgb, var(--theme-color) 10%, transparent);
  
  &:last-child {
    border-bottom: none;
  }
}

.suggestion-item:hover {
  background: color-mix(in srgb, var(--theme-color) 10%, transparent);
}

.title {
  font-weight: 500;
  color: var(--primary-color);
}

.artist {
  color: color-mix(in srgb, var(--primary-color) 60%, transparent);
  font-size: 0.9em;
}
</style> 